<?php
/**
 * Created by PhpStorm.
 * User: hjd-yao
 * Date: 2018/9/28
 * Time: 14:13
 */

namespace App\Helpers;


use App\Helpers\Traits\HandleError;

/**
 * 启瑞云短信接口Demo(utf-8)
 */
class SendSMS
{
    use HandleError;
    const SENDURL = 'http://api.qirui.com:7891/mt';
    const SIGNNAME = '{爱心联盟}';

    private $apiKey;
    private $apiSecret;

    /**
     * 构造方法
     * @param string $apiKey    接口账号
     * @param string $apiSecret 接口密码
     */
    public function __construct($apiKey, $apiSecret)
    {
        $this->apiKey    = $apiKey;
        $this->apiSecret = $apiSecret;
    }

    public static function make()
    {
        return new static(config('sms.qiruiyuan.apiKey'),config('sms.qiruiyuan.apiSecret'));
    }

    public function sendCode($mobile,$code)
    {
        if (!config('sms.is_sms')){
            $this->errorMsg = '短信功能暂未开启，请直接进行下一步操作！';
            return false;
        }

        $content = self::SIGNNAME."您的验证码为{%s}，请不要告知他人";
        $content = sprintf($content,$code);
        if($this->hasSend($mobile)){
            $this->errorMsg = '该号码已发送过短信，请稍后再试';
            return false;
        }
        $res = $this->send($mobile,$content);
        if($res){
            $this->saveCodeCache($mobile,$code);
            return true;
        }
        return false;
    }

    public function saveCodeCache($mobile,$code)
    {
        $ip = get_client_ip(1);
        $data['mobile'] = $mobile;
        $data['code'] = $code;
        $data['ip'] = $ip;
        $data['time'] = time();
        cache()->put(md5($mobile),$data,300);
    }

    public function limitCount($mobile)
    {
        $ip = get_client_ip(1);
        $data = cache()->get(md5($ip));
    }

    public function hasSend($mobile)
    {
        $data = cache()->get(md5($mobile));
        if(!$data){
            return false;
        }
        if($data['mobile'] == $mobile&&$data['time']+60>time()){
            return true;
        }
        return false;
    }

    public function checkCode($code,$mobile)
    {
        if (!config('sms.is_sms')){
            $this->errorMsg = '短信功能暂未开启，请直接操作！';
            return true;
        }
        if(!$code||!$mobile){
            $this->errorMsg = '手机号或验证码不能为空';
            return false;
        }
        $ip = get_client_ip(1);
        $data = cache()->get(md5($mobile));
        if(!$data){
            $this->errorMsg = '手机号原发送短信的号码不一致';
            return false;
        }
        if($data['mobile']!=$mobile||$data['code'] != $code){
            $this->errorMsg = '手机号或验证码不正确';
            return false;
        }
        return true;
    }

    /**
     * 短信发送
     * @param string $phone   	手机号码
     * @param string $content 	短信内容
     * @param integer $isreport	是否需要状态报告
     * @return void
     */
    public function send($phone, $content, $isreport = 1)
    {
        $requestData = array(
            'un' => $this->apiKey,
            'pw' => $this->apiSecret,
            'sm' => $content,
            'da' => $phone,
            'rd' => $isreport,
            'dc' => 15,
            'rf' => 2,
            'tf' => 3,
        );

        $url = self::SENDURL . '?' . http_build_query($requestData);
        return $this->request($url);
    }

    /**
     * 请求发送
     * @return string 返回发送状态
     */
    private function request($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        $result = curl_exec($ch);
        curl_close($ch);
        return $this->handleResponseInfo($result);
    }

    private function handleResponseInfo($response){
        $info=json_decode($response,true);
        if($info['success'] ===true){
            return true;
        }
        return $info;
    }
}


